package com.bayes.teamshow.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bayes.teamshow.pojo.User;
import com.bayes.teamshow.service.UserImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/html")
public class BaseHtmlCon {

	@Autowired
	UserImpl userImpl;

	@RequestMapping("/markdown")
	public String getMarkdown() {
		return "markdown";
	}

	@RequestMapping("/login")
	public String getLogin() {
		return "login";
	}

	@RequestMapping("/test")
	public String getTest() {
		return "test";
	}

	@RequestMapping("/getuser")
	@ResponseBody
	public Object getUser() {
		return userImpl.getUser();
	}


	@RequestMapping("/json")
	@ResponseBody
	public Object getJson() {
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(new User(1, "asd", "sdfas"));
		jsonArray.add(new User(2, "asd1", "sdfas2"));
		jsonObject.put("result", "ok");
		jsonObject.put("mes", "这是服务器端的数据");
		jsonObject.put("data", jsonArray);
		return jsonObject;
	}

	// 传参

	// user 表单数据直接传参到user对象
	@RequestMapping("/loginisright")
	public String loginisright(User user) {
		User user1 = userImpl.findUser(user);
		if (user1 == null) {
			//密码不对就返回登录
			return "login";
		} else return "index";
	}


	// get url直接传参,或者post传参
	//http://localhost:5001/html/data?name=jia&passwd=1234
	@RequestMapping("/data")
	@ResponseBody
	public Object getData(@RequestParam("name") String name, @RequestParam("passwd") String passwd) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", name);
		return jsonObject;
	}

	// post请求
	@RequestMapping("/datarequ")
	@ResponseBody
	public Object getData(HttpServletRequest httpServletRequest) {

		String name = httpServletRequest.getParameter("name");
		String passwd = httpServletRequest.getParameter("passwd");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", name);
		jsonObject.put("url", "login");
		return jsonObject;
	}


}
